﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Image2d
{
    class ShotManager
    {
        Vector2 position;
        Texture2D image;
        Animation[] animation;
        float interval = 200f;
        float lastShot;
        float rotation;
        float velocity;
        Animation[] specialAnimations;

        Vector2 clientbounds;

        List<Shot> shots;
        public List<Shot> Shots
        {
            get { return shots; }
            set { shots = value; }
        }

        public ShotManager(Texture2D image, Vector2 position,
            Animation[] animation, Animation[]specialAnimations, Vector2 bounds)
        {
            this.image = image;
            this.position = position;
            this.animation = animation;
            this.specialAnimations = specialAnimations;
            clientbounds = bounds;
            shots = new List<Shot>();
        }

        public void AddShot(GameTime gameTime)
        {

            Console.WriteLine("New SHOOOOOOOT");
            
           if (lastShot > (float)gameTime.TotalGameTime.TotalMilliseconds 
                  && lastShot != 0)
            {
                Console.WriteLine("OR NOT");
                return;
            }
            lastShot = (float)gameTime.TotalGameTime.TotalMilliseconds + interval;

            shots.Add(new Shot(image, position, animation,
                                rotation, velocity,(float)gameTime.TotalGameTime.TotalMilliseconds,
                                clientbounds, "normal"));
        }

        public void AddSpecial(GameTime gameTime)
        {
            if (lastShot > (float)gameTime.TotalGameTime.TotalMilliseconds
                 && lastShot != 0)
            {
                Console.WriteLine("OR NOT");
                return;
            }
            lastShot = (float)gameTime.TotalGameTime.TotalMilliseconds + interval*10;
            shots.Add(new Shot(image, position, specialAnimations,
                                rotation, velocity,(float)gameTime.TotalGameTime.TotalMilliseconds,
                                clientbounds*3, "special"));
        }

        public void Update(GameTime gameTime, Player player)
        {
            this.velocity = player.Velocity + player.VelocityMaximun;
            this.position = player.Position;
            this.rotation = player.Rotation;
            for (int i = shots.Count - 1; i >= 0; i--)
            {
                if (shots[i].CheckUpdate(gameTime,position,rotation))
                {
                    shots.RemoveAt(i);
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            for (int i = shots.Count - 1; i >= 0; i--)
            {
                shots[i].Draw(spriteBatch, gameTime);
            }
        }
    }
}
